package com.sgu6.sgu6_machine.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.dto.ErrorCodeDto;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ErrorCodeAdapter extends RecyclerView.Adapter implements View.OnClickListener {


    private List<ErrorCodeDto> codes;
    private Context mContext;

    public ErrorCodeAdapter(Context mContext, List<ErrorCodeDto> codes) {
        this.codes = codes;
        this.mContext = mContext;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_error_code, viewGroup, false);
        v.setOnClickListener(this);
        return new Holder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        holder.itemView.setTag(i);
        if (holder instanceof Holder) {
            ((Holder) holder).tvCode.setText("[" + codes.get(i).code + "]");
            ((Holder) holder).tvMsg.setText("[" + codes.get(i).msg + "]");
        }
    }

    @Override
    public int getItemCount() {
        return codes.size();
    }

    @Override
    public void onClick(View v) {
//        listener.onItemClick((int) v.getTag());
    }


    static class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_code)
        TextView tvCode;
        @BindView(R.id.tv_msg)
        TextView tvMsg;

        Holder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setOnClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}
